package com.css.controller;

import com.css.Main;
import com.css.entity.CustomShortcutKeyName;
import com.css.listener.MyKeyCode;
import com.css.view.CustomShortcutKeyInput;
import javafx.collections.ObservableList;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.VBox;

import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;

/**
 * <p>
 * -
 * </p>
 *
 * @author css
 * @since 2021/4/7
 */
public class SettingController extends BaseController implements Initializable {

    public Label status;
    public VBox customShortcutKey;

    public SettingController() {
        System.out.println("SettingController");
    }

    public void changeStatus(MouseEvent mouseEvent) {
        mouseEvent.consume();
        setStatusText(Main.getMonitor().startOrStop());
    }

    public void draggedStatus(MouseEvent mouseEvent) {
        mouseEvent.consume();
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        System.out.println("SettingController.initialize");
        Main.getUser((user) -> {
            for (CustomShortcutKeyName customShortcutKeyName : user.getCustomShortcutKeyNameList()) {
                CustomShortcutKeyInput shortcutKey = new CustomShortcutKeyInput(customShortcutKeyName);
                run(() -> customShortcutKey.getChildren().add(shortcutKey));
            }
        });
        setStatusText(Main.getMonitor().isOpen());
    }

    public void setStatusText(boolean isOpen) {
        status.setText(isOpen ? "开启" : "关闭");
    }

    public void addCustomShortcutKey(MouseEvent mouseEvent) {
        CustomShortcutKeyInput shortcutKey = new CustomShortcutKeyInput(new CustomShortcutKeyName());
        customShortcutKey.getChildren().add(shortcutKey);
    }

    public void saveSetting(MouseEvent mouseEvent) {
        ObservableList<Node> children = customShortcutKey.getChildren();
        for (Node child : children) {
            List<MyKeyCode> keyCodeList = ((CustomShortcutKeyInput) child).getKeyCodeList();
            System.out.println(keyCodeList);
        }
    }
}
